fix: handle premature stream termination for Anthropic (#1868)#2047
fix: handle premature stream termination for Anthropic (#1868)#2047gautamsirdeshmukh merged 1 commit intostrands-agents:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Looking into why 1-3 integ tests continue to time out intermittently (this change to the Anthropic stream method has zero connection to the failed cases for multi-agent executions, concurrency for tests may need to be optimized). Edit: Looks like #2044 is seeing the same test failure. Certainly unrelated to either change, as expected. |
|
/strands review |
|
/strands are there any issues with this change? |
|
/strands review |
|
Assessment: Request Changes Good fix for a real crash scenario. The approach of using Review Details
The core fix is solid and the thorough scenario testing in the PR description is appreciated. |
|
/strands review |
|
Assessment: Approve All previous review feedback has been addressed cleanly. The exception catch is now scoped to Review Summary
Solid fix with thorough testing. |
|
/strands review |
|
Assessment: Approve All prior review feedback has been addressed. No new issues found. Review Details
|
|
Assessment: Approve This PR has been merged. Confirming the final state looks good — no new issues found. Review Details
|
The test_stream_message_stop_no_pydantic_warnings test was using a raw async generator as the stream mock, which lacks get_final_message(). This caused an AttributeError on Python 3.14 after PR strands-agents#2047 introduced stream.get_final_message() in the Anthropic provider. Switch to generate_mock_stream_context helper which properly mocks the stream object with get_final_message support.
Problem
The Anthropic provider's stream method tries to read
event.message.usagefrom the last iterated stream event to extract token usage metadata. However, if the stream terminates prematurely and the last stream event's.messageattribute has not yet been populated with usage, this line crashes with an AttributeError.Solution
Instead of checking
event.message.usagefor the last stream event, we now call Anthropic SDK'sstream.get_final_message()method, which returns a "message snapshot" accumulated from all received events rather than relying on the last event's state. This call is wrapped in a try/except/else block, so that if it fails (which is only possible when zero events were received) we log a warning instead of crashing.Possible Concerns
One may worry that merely logging a warning when
get_final_message()call fails could lead to undercounted token usage. However, this method only fails when the stream yields zero events - in which case, there is no usage data to report anyway. If one or more events were received, the Anthropic SDK guarantees that the snapshot contains usage data (initialized by the mandatorymessage_startevent), andget_final_message()will succeed.Related Issues
#1868
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepare(added a few tests to cover premature termination + empty stream cases)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.